package org.springblade.modules.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BaseEntity;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tenant.annotation.TenantIgnore;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.GroupRobotConfigDTO;
import org.springblade.modules.admin.entity.ChatGroup;
import org.springblade.modules.admin.entity.GroupRobotConfig;
import org.springblade.modules.admin.enums.ChatGroupRobotEnum;
import org.springblade.modules.admin.enums.GroupRobotEnum;
import org.springblade.modules.admin.mapper.GroupRobotConfigMapper;
import org.springblade.modules.admin.service.GroupRobotConfigService;

import org.springblade.modules.admin.service.IChatGroupService;
import org.springblade.modules.admin.vo.GroupRobotConfigVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Sharry
* @description 针对表【blade_group_robot_config(消息推送-群-机器人配置表)】的数据库操作Service实现
* @createDate 2023-10-09 16:43:13
*/
@Service
public class GroupRobotConfigServiceImpl extends ServiceImpl<GroupRobotConfigMapper, GroupRobotConfig>
    implements GroupRobotConfigService {

	@Autowired
	@Lazy
	private IChatGroupService chatGroupService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean configBatch(GroupRobotConfigDTO groupRobotConfigDTO) {
		boolean isSuccess = true;
		List<Long> groupIds = groupRobotConfigDTO.getGroupIds();
		List<GroupRobotConfig> configs = groupRobotConfigDTO.getConfigs();
		// 1. 参数校验
		if(Func.isEmpty(groupIds)){
			throw new ServiceException("请勾选需要批量操作的群");
		}
		// 2. 全量删除原本组id的配置
		for (Long groupId : groupIds) {
			List<GroupRobotConfig> tempList = this.list(new LambdaQueryWrapper<GroupRobotConfig>()
				.eq(GroupRobotConfig::getChatgroupId, groupId)
				.eq(BaseEntity::getIsDeleted, 0)
			);
			if(Func.isNotEmpty(tempList)){
				List<Long> batchIds = tempList.stream()
					.map(GroupRobotConfig::getId)
					.collect(Collectors.toList());
				isSuccess = isSuccess && this.removeByIds(batchIds);
			}
		}
		// 若为空，表示设置为全否，直接返回
		if (Func.isEmpty(configs)){
			return true;
		}
		// 3. 配置
		for (Long groupId : groupIds) {
			String tenantId = chatGroupService.getById(groupId).getTenantId();
			configs.forEach(c ->{
				c.setChatgroupId(groupId);
				c.setId(null);
				c.setCreateUser(AuthUtil.getUserId());
				c.setUpdateUser(AuthUtil.getUserId());
				c.setCreateTime(new Date());
				c.setUpdateTime(new Date());
				c.setIsDeleted(0);
				c.setTenantId(tenantId);
			} );
			isSuccess = isSuccess && this.saveBatch(configs);
		}
		return isSuccess;
	}

	/**
	 * 根据当前电梯 获取推送配置
	 * @param elevatorId 当前电梯
	 * @return 封装的结果
	 */
	@Override
	public List<GroupRobotConfigVO> getConfigByElevatorId(Long elevatorId){

		List<GroupRobotConfigVO> result = baseMapper.getConfigByElevatorId(elevatorId);
		if(Func.isNotEmpty(result)){
			result = result.stream()
				.filter(f -> f.getPushAllow().equals(ChatGroupRobotEnum.GROUP_PUSH_OPEN.getKey()))
				.collect(Collectors.toList());
		}
		return result;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	@TenantIgnore
	public boolean init() {
		boolean isSuccess;
		// 1. 获取当前群
		List<ChatGroup> groupList = chatGroupService.list(new LambdaQueryWrapper<ChatGroup>()
			.eq(BaseEntity::getIsDeleted,0)
		);
		List<Long> groupIds = new ArrayList<>();
		if(Func.isNotEmpty(groupList)){
			groupIds = groupList.stream().map(ChatGroup::getId).collect(Collectors.toList());
		}
		List<GroupRobotConfig> configs = defaultConfigs();
		GroupRobotConfigDTO groupRobotConfigDTO = new GroupRobotConfigDTO();
		groupRobotConfigDTO.setGroupIds(groupIds);
		groupRobotConfigDTO.setConfigs(configs);
		isSuccess = this.configBatch(groupRobotConfigDTO);
		// 2. 设置默认值
		return isSuccess;
	}

	public List<GroupRobotConfig> defaultConfigs(){
		List<GroupRobotConfig> configs = new ArrayList<>(10);
		GroupRobotConfig repairSend = new GroupRobotConfig();
		repairSend.setStatus(GroupRobotEnum.REPAIR_SEND.getStatus());
		repairSend.setPushType(GroupRobotEnum.REPAIR_SEND.getPushType());
		GroupRobotConfig repairBegin = new GroupRobotConfig();
		repairBegin.setStatus(GroupRobotEnum.REPAIR_BEGIN.getStatus());
		repairBegin.setPushType(GroupRobotEnum.REPAIR_BEGIN.getPushType());
		GroupRobotConfig repairFinish = new GroupRobotConfig();
		repairFinish.setStatus(GroupRobotEnum.REPAIR_FINISH.getStatus());
		repairFinish.setPushType(GroupRobotEnum.REPAIR_FINISH.getPushType());
		GroupRobotConfig maintainBegin = new GroupRobotConfig();
		maintainBegin.setStatus(GroupRobotEnum.MAINTAIN_BEGIN.getStatus());
		maintainBegin.setPushType(GroupRobotEnum.MAINTAIN_BEGIN.getPushType());
		GroupRobotConfig maintainFinish = new GroupRobotConfig();
		maintainFinish.setStatus(GroupRobotEnum.MAINTAIN_FINISH.getStatus());
		maintainFinish.setPushType(GroupRobotEnum.MAINTAIN_FINISH.getPushType());

		configs.add(repairSend);
		configs.add(repairBegin);
		configs.add(repairFinish);
		configs.add(maintainBegin);
		configs.add(maintainFinish);

		return configs;
	}
}
